//
//  NSString+category.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/7.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "NSString+category.h"

@implementation NSString (category)
//判断输入的文字是不是数字
+ (BOOL)judgeInputIsNumber:(NSString *)str
{
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:str]) {
        return YES;
    }
    return NO;
}
//判断是否是纯汉字
- (BOOL)isChinese{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}
//判断是否含有汉字
- (BOOL)includeChinese{
    for(int i=0; i< [self length];i++){
        int a =[self characterAtIndex:i];
        if( a >0x4e00&& a <0x9fff){
            return YES;
        }
    }
    return NO;
}

//每四个字符之间，加一个空格
+ (NSString *)spaceBetweenFourCharacters:(NSString *)string{
    if (string.length > 16)
        return [self startString:[string substringToIndex:16] character:@" " perIndex:4];
    else
        return [self startString:string character:@" " perIndex:4];
    //    if (string.length <= 4)
    //        return string;
    //    else if (string.length > 16)
    //        return [self spaceBetweenFourCharacters:[string substringToIndex:16]];
    //    else
    //        return [NSString stringWithFormat:@"%@ %@",[string substringToIndex:4],
    //                [self spaceBetweenFourCharacters:[string substringFromIndex:4]]];
}

//每三个字符之间，加一个逗号
+ (NSString *)commaBetweenThreeCharacters:(NSString *)string{
    return [self startString:string character:@"," perIndex:3];
    //    if (string.length <= 3)
    //        return string;
    //    else
    //        return [NSString stringWithFormat:@"%@,%@",[self commaBetweenThreeCharacters:[string substringToIndex:string.length - 3]],
    //                [string substringFromIndex:string.length - 3]];
}
/**
 *@pragma 字符串之间，用特殊符号隔开
 */
+ (NSString *)startString:(NSString *)string character:(NSString *)character perIndex:(int)index{
    if (string.length <= index)
        return string;
    else
        return [NSString stringWithFormat:@"%@%@%@",[string substringToIndex:index],
                character,
                [self startString:[string substringFromIndex:index] character:character perIndex:index]];
}

//把数组转换成特殊的字符串格式，比如@[@"1",@"2",@"1234567",@"3"]  -> 1|2|1,2,3,4,5,6|3
+ (NSString *)specialStringWithArray:(NSArray *)array arrayCharacter:(NSString *)arrayCharacter stringCharacter:(NSString *)stringCharacter{
    NSMutableArray *mutableArr = [NSMutableArray array];
    for (NSString *string in array) {
        [mutableArr addObject:[NSString startString:string character:stringCharacter perIndex:1]];
    }
    return [mutableArr componentsJoinedByString:arrayCharacter];
}

//少于两位数的字符串，自动在前面补0（应用于时间格式）
+ (NSString *)doubleString:(NSString *)string{
    if (string.length != 1)
        return string;
    return [@"0" stringByAppendingString:string];
}

//加上@“第-期”字样
+ (NSString *)getDateNumber:(NSString *)string{
    if (string.length < 8)
        return [NSString stringWithFormat:@"第%@期",string];
    return [NSString stringWithFormat:@"第%@-%@期", [string substringToIndex:8], [string substringFromIndex:8]];
}
//截止日期
+ (NSString *)getClosingDate:(NSString *)string{
    if (string.length < 8)
        return [NSString stringWithFormat:@"距离%@期截止:",string];
    return [NSString stringWithFormat:@"距离%@期截止:",[string substringFromIndex:8]];
}

//无符号长整型转换成字符串的时间格式
+ (NSString *)getTime:(NSUInteger)number{
    NSUInteger hours = number / 3600;
    NSUInteger minutes = number % 3600 / 60;
    NSUInteger seconds = number % 60;
    NSString *hourStr = (hours < 10)?[NSString stringWithFormat:@"0%lu", hours]:[NSString stringWithFormat:@"%lu", hours];
    NSString *minuteStr = (minutes < 10)?[NSString stringWithFormat:@"0%lu", minutes]:[NSString stringWithFormat:@"%lu", minutes];
    NSString *secondStr = (seconds < 10)?[NSString stringWithFormat:@"0%lu", seconds]:[NSString stringWithFormat:@"%lu", seconds];
    return [NSString stringWithFormat:@"%@:%@:%@", hourStr, minuteStr, secondStr];
}

//把时间格式的字符串，转变成整数型
+ (NSUInteger)getIntergerTime:(NSString *)time{
    NSArray *array = [time componentsSeparatedByString:@":"];
    NSUInteger timeNum = 0;
    if ([array[0] integerValue] > 0)
        timeNum = [array[0] integerValue] * 3600 + timeNum;//小时位
    else if ([array[1] integerValue] > 0)
        timeNum = [array[1] integerValue] * 60 + timeNum;//分位
    else if ([array[2] integerValue] > 0)
        timeNum = [array[2] integerValue] + timeNum;
    return timeNum;
    
}
//字符串转换成数组，有的中奖号码是用逗号隔开的字符串
+ (NSArray *)arrayWithString:(NSString *)string{
    if (string.length == 0)
        return nil;
    else{
        NSMutableArray *array = [NSMutableArray arrayWithArray:[string componentsSeparatedByString:@","]];
        if ([array[array.count - 1] isEqualToString:@""])
            [array removeLastObject];
        return array;
    }
}

//月／日／时／分的字符串时间格式，转换成 年／月／日／时／分的字符串时间格式
+ (NSString *)insertYearWithDateString:(NSString *)string{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY"];//
    NSDate *date = [NSDate date];
    NSString *year = [formatter stringFromDate:date];//年份
    
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"月日时分"];
    NSArray *array  = [string componentsSeparatedByCharactersInSet:set];
    
    for (int i = 0; i < array.count; i ++) {
        NSString *subStr = [NSString doubleString:array[i]];
        if (i < 2)
            year = [year stringByAppendingString:[NSString stringWithFormat:@"-%@",subStr]];
        else if (i == 2)
            year = [year stringByAppendingString:[NSString stringWithFormat:@" %@",subStr]];
        else if (i == 3)
            year = [year stringByAppendingString:[NSString stringWithFormat:@":%@",subStr]];
    }
    return year;
}
//+ (NSString *)stringWithArray:(NSArray *)array{
//    if (!array)
//        return nil;
//    else if([array[0] isKindOfClass:[NSDictionary class]]){
//        for (NSDictionary *dict in array) {
//            id value = [dict allValues];
//            if ([value isKindOfClass:[NSSet class]])
//                return [self stringWithArray:[value allObjects]];
//            else if ([value isKindOfClass:[NSArray class]])
//                return [self stringWithArray:value];
//        }
//    }
//    else{
//        NSMutableString * mustableStr = [NSMutableString string];
//        for (NSString *string in array) {
//            [mustableStr stringByAppendingString:[NSString stringWithFormat:@"%@,",string]];
//        }
//        return [mustableStr substringToIndex:mustableStr.length - 1];
//    }
//    return nil;
//}
//求和
+ (int)sum:(NSString *)string{
    return [NSArray sum:[NSString arrayWithString:string]];
}
//球差
+ (int)minus:(NSString *)string{
    return [NSArray minus:[NSString arrayWithString:string]];
}

@end
